
//
//  ArtGalleryViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/24.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "ArtGalleryViewController.h"
#import "SliderViewController.h"
#import "CollectViewController.h"
#import "MyArtViewController.h"
#import "SiMiViewController.h"
#import "TZImagePickerController.h"
#import "IGGetUploadApi.h"
@interface ArtGalleryViewController () <SliderViewControllerDelegate> {
    
    MyArtViewController *vc2;
    SiMiViewController *vc1;
    UIButton *right;
    BOOL isEdit;
    BOOL isChange;
    
    NSString *secrecy;//1公开, 2保密
    NSString *pageNumber;//页数，从0开始
    NSString *pageSize;//一页显示列表数
    NSString *queryTime;//查询时间
}

@property (nonatomic, strong) SliderViewController *sliderVC;
@property (nonatomic, strong) IGGetUploadApi *uploadApi;

@end

@implementation ArtGalleryViewController
@synthesize sliderVC;

#pragma mark-生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"My Upload", nil);

    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 60, 40);
    [right setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateSelected];
    [right setTitle:NSLocalizedString(@"Select", nil) forState:UIControlStateNormal];
    [right.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    [right setTitleColor:RGB(0, 170, 229) forState:UIControlStateNormal];
    [right addTarget:self action:@selector(canel:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    [self setIsEdit:NO];
    
    sliderVC = [[SliderViewController alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT-NavigationMagin)];
    sliderVC.slideSwitchViewDelegate = self;
    sliderVC.shadowImageView.backgroundColor=[UIColor whiteColor];
    sliderVC.tabItemNormalColor = RGB(115, 115, 115);
    sliderVC.tabItemSelectedColor = [UIColor blackColor];
    [self.view addSubview:sliderVC];
    
    [sliderVC buildUI];
    [self loadModel];
    if (self.index)
    {
        [sliderVC selectIndex:self.index];
    }
}
- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
    }
    if (sliderVC) {
        [sliderVC selectIndex:index];
        [self loadModel];
    }
}

- (void)upDataSucc:(NSNotification *)not
{
    BOOL isPublic = [not.object boolValue];
    self.index = isPublic ? 1 : 0;
}

- (void)setIsEdit:(BOOL)edit
{
    [right setSelected:edit];
    isEdit = edit;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    if (sliderVC.userSelectedChannelID == 100)
    {
        [vc1 scrollToTOP];
    }
     if (sliderVC.userSelectedChannelID == 101)
    {
        [vc2 scrollToTOP];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coverWindowClick) name:@"click" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"click" object:nil];
}

#pragma mark - SliderViewControllerDelegate

- (NSUInteger)numberOfTab:(SliderViewController *)view {
    return 2;
}

- (UIViewController *)slideSwitchView:(SliderViewController *)view viewOfTab:(NSUInteger)number {
    __weak ArtGalleryViewController *weakSelf = self;
    if (number == 0)
    {
         vc1 = [[SiMiViewController alloc] init];
        vc1.title = NSLocalizedString(@"Private", nil);
        [vc1 setEditBlock:^(BOOL isCancel){
            [weakSelf changeRightItemState:isCancel];
        }];
        vc1.nav = self.navigationController;
        return vc1;
    }
    else if (number == 1)
    {
        vc2 = [[MyArtViewController alloc] init];
        vc2.title = NSLocalizedString(@"Public", nil);
        [vc2 setEditBlock:^(BOOL isCancel){
            [weakSelf changeRightItemState:isCancel];
        }];
        vc2.nav = self.navigationController;
        return vc2;
    }
    return nil;
}


- (void)changeRightItemState:(BOOL)cancle
{
    if (cancle)
    {
        [right setSelected:YES];
        self.sliderVC.rootScrollView.scrollEnabled = NO;
        self.sliderVC.topScrollView.userInteractionEnabled = NO;
    }
    else
    {
        [right setSelected:NO];
        self.sliderVC.rootScrollView.scrollEnabled = YES;
        self.sliderVC.topScrollView.userInteractionEnabled = YES;
    }
}

- (void)canel:(UIButton *)btn
{
    if ([right isSelected]) {
        [self setIsEdit:NO];
        sliderVC.rootScrollView.scrollEnabled = YES;
        sliderVC.topScrollView.userInteractionEnabled = YES;
    }else if(![right isSelected]){
        [self setIsEdit:YES];
        sliderVC.rootScrollView.scrollEnabled = NO;
        sliderVC.topScrollView.userInteractionEnabled = NO;
    }
    if (sliderVC.userSelectedChannelID == 100) {
        [vc1 canel];
        return;
    }
    if (sliderVC.userSelectedChannelID == 101) {
        [vc2 canel];
        return;
    }
}

- (void)loadModel
{
    [vc1 viewDidLoad];
    [vc2 viewDidLoad];
}

- (void)coverWindowClick
{
    
    if (sliderVC.userSelectedChannelID == 100)
    {
        [vc1 scrollToTOP];
    }
    else if (sliderVC.userSelectedChannelID == 101)
    {
        [vc2 scrollToTOP];
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//     sliderVC.slideSwitchViewDelegate =nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
